package com.dong.mcd.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dong.mcd.dao.MusicHisMapper;
import com.dong.mcd.model.MusicHis;
import com.dong.mcd.model.MusicHisExample;
import com.dong.mcd.model.MusicHisExample.Criteria;
import com.dong.mcd.service.IMusicService;
import com.dong.mcd.util.PageConfig;

@Transactional
@Service("musicService")
public class MusicService implements IMusicService {

	@Autowired
	private MusicHisMapper musicMapper;

	public MusicHis getMusic(String id) {
		return musicMapper.selectByPrimaryKey(id);
	}

	public void removeByIds(String[] ids) {
		if (ids != null)
			for (int i = 0; i < ids.length; i++) {
				musicMapper.deleteByPrimaryKey(ids[i]);
			}
	}

	public PageConfig search(String name, Integer srcType, int pageIndex,
			int pageSize) {
		MusicHisExample example = new MusicHisExample();
		Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(name))
			criteria.andNameLike("%"+name+"%");
		if (srcType != null)
			criteria.andSrcTypeEqualTo(srcType);
		return new PageConfig(musicMapper, example, pageIndex, pageSize);
	}

	public void store(MusicHis music) {
		if (music == null)
			return;
		MusicHis bean = getMusic(music.getId());
		if (bean != null) {
			musicMapper.updateByPrimaryKey(music);
		} else {
			musicMapper.insert(music);
		}
	}

}
